<?php
class ManagePromotion extends Module
{
	var $hotel = array();
	
	function ManagePromotion($row)
	{
		Module::Module($row);


        
        #############################################################################
        //Check quyền
		if(!User::have_permit("hotel_edit"))
        {
        	if(!Db_Hotel::have_permit_manage($this->hotel, User::id()))
	        {
	        	Url::access_denied();
	        }
        }
        #############################################################################
        
		switch(Url::get('cmd'))
   		{
   			case 'edit':
                CGlobal::$website_title = "Sửa chương trình khuyến mãi";
                
                require_once "forms/ManagePromotionEdit.php";
                $this->add_form(new ManagePromotionEditForm);
                
                break;
            case 'add':
                CGlobal::$website_title = "Thêm chương trình khuyến mãi";
                
                require_once "forms/ManagePromotionAdd.php";
                $this->add_form(new ManagePromotionAddForm);
                
                break;
                
          	case 'delete':
          		$id = Url::getInt('id', 0);
                
                if($id)
                {
                	$Promotion = Db_Promotion::getPromotionById($id);
                	
	                if($Promotion && $Promotion['status'] != -1 && $Promotion['hotel_id'] == $this->hotel['id'] && User::have_permit("hotel_edit"))
                	{
	                	Db_Promotion::delPromotionById($id);
	                }
                }
                
                $href = Url::get('href');
            
	            if($href)
	            {
	            	Url::redirect_url($href);
	            }
	            else 
	            {
	            	Url::redirect_current(array('hotel_id'));
	            }
                
                break;
                
            case 'show':
                $status=1;
                
            case 'hide':
            	$href = Url::get('href');
            	//echo $href;exit;
            	 
                if(!isset($status))
                {
                    $status		=	2;
                }
                
                $id = Url::getInt('id', 0);
                
                if($id)
                {
                	$Promotion = Db_Promotion::getPromotionById($id);
                	
                	if($Promotion && $Promotion['status'] != -1 && $Promotion['status'] != $status && $Promotion['hotel_id'] == $this->hotel['id'] && (User::have_permit("hotel_edit") || Db_Hotel::have_permit_manage($this->hotel,User::id())))
                	{
                		Db_Promotion::update(array('status'=>$status),$id);
                	}
                }
                
                $href = Url::get('href');
            
	            if($href)
	            {
	            	Url::redirect_url($href);
	            }
	            else 
	            {
	            	Url::redirect_current(array('hotel_id'));
	            }
                
                break;
            case 'restore':
                $id = Url::getInt('id', 0);
                
                if($id)
                {
                	$Promotion = Db_Promotion::getPromotionById($id);
                	
                	if($Promotion && $Promotion['status'] == -1 && $Promotion['hotel_id'] == $this->hotel['id'] && User::have_permit("hotel_edit"))
                	{
                		Db_Promotion::update(array('status'=>1),$id);
                	}
                }
                
                $href = Url::get('href');
            
	            if($href)
	            {
	            	Url::redirect_url($href);
	            }
	            else 
	            {
	            	Url::redirect_current(array('hotel_id'));
	            }
	            
                break;    
            default:
                require_once "forms/ManagePromotionList.php";
				$this->add_form(new ManagePromotionListForm());
                break;
		}
	}
}
?>